package com.xq.sqlSession;

import com.xq.config.XmlConfiguration;
import com.xq.pojo.Configuration;
import org.dom4j.DocumentException;
import org.xml.sax.SAXException;

import java.beans.PropertyVetoException;
import java.io.InputStream;

/**
 * @Author: xiaoqiang
 * @Description: 解析配置文件，并且生产sqlSession
 * @Date: 2021/5/28
 * @Version: 1.0
 */
public class SqlSessionBuilder {

    /**
     * 将配置文件转为 SqlSessionFactory
     *
     * @return
     */
    public SqlSessionFactory build(InputStream in) throws DocumentException, PropertyVetoException, SAXException {
//        step1 使用dom4j 解析配置文件
        XmlConfiguration xmlConfiguration = new XmlConfiguration();
        Configuration configuration = xmlConfiguration.xml2Config(in);

//        创建sqlsessionFactory，生产sqlsession
        DefaultSqlSessionFactory defaultSqlSessionFactory = new DefaultSqlSessionFactory(configuration);

        return defaultSqlSessionFactory;
    }
}
